package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "预约管理")
public class ReservationController{
    @Autowired
    private ReservationService reservationService;
    /**
     * 2.1 查询取消预约数量
     * - 接口地址:/customer/reservation/cancelled-count
     * - 请求方式:GET
     * - 请求参数:
     * 无，我们需要获取线程中的用户即可，在后端获取，不需要前端传任何参数0
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledCount() {
        Integer count = reservationService.getCancelledCount();
        return ResponseResult.success(count);
    }
    /**
     * 2.2 查询每个时间段剩余预约次数
     * - 接口地址:/customer/reservation/countByTime
     * - 请求方式:GET
     * - 请求参数:
     */

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult add(@RequestBody ReservationDto dto) {
        reservationService.add(dto);
        return ResponseResult.success();
    }

    /**
     * 2.4 分页查询预约
     * - 接口地址:/customer/reservation/page
     * - 请求方式:GET
     * - 请求参数:
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "int", paramType = "query"),
    })
    public ResponseResult<PageResponse<ReservationVo>> findByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize,
                                                                  @RequestParam(required = false) Integer status){
        PageResponse<ReservationVo> byPage = reservationService.findByPage(pageNum, pageSize,status);
        return ResponseResult.success(byPage);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancel(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return ResponseResult.success();
    }
    /**
     * - 接口地址:/customer/reservation/countByTime
     * - 请求方式:GET
     * - 请求参数:
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<Integer>> countByTime(Long time) {

        List<Map<String, Object>> counts = reservationService.countByTime(time);

        return ResponseResult.success(counts);
    }

}
